package com.aric.springbootmusic.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Autowired
    private FileUploadInfo fileUploadInfo;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //配置跨域
        registry.addMapping("/**")
                //放行那些原始域
                .allowedOriginPatterns("*")
                .allowedMethods(new String[]{"GET","POST","PUT","DELETE"})
                .allowedHeaders("*")
                .exposedHeaders("*");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String audioSuffix = fileUploadInfo.getAudioFileHttpUrlAddressSuffix();
        String picSuffix = fileUploadInfo.getPicFileHttpUrlAddressSuffix();

        String picAddress = fileUploadInfo.getPicFileRealAddress();
        String audioAddress = fileUploadInfo.getAudioFileRealAddress();
        //静态文件的虚拟路径和实际路径之间的映射
        registry.addResourceHandler(picSuffix+"**")
                .addResourceLocations("file:"+picAddress);
        registry.addResourceHandler(audioSuffix+"**")
                .addResourceLocations("file:"+audioAddress);
    }


}
